package cn.liontalk.demo;

/**
 * @author: 周哲
 * @package: cn.liontalk.demo
 * @description: 同时访问一个类的不同的普通同步方法
 * @date: 2019/1/26 15:26
 * @version: V1.0
 */
public class SynchronizedDifferentMethod7 implements Runnable {

    static SynchronizedDifferentMethod7 instance = new SynchronizedDifferentMethod7();


    public void run() {
       if(Thread.currentThread().getName().equals("Thraad-0")){
           method();
       }else{
           method2();
       }

       /* method();
        method2();*/
    }


    public synchronized void method() {
        System.out.println("加锁的方法1：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加锁方法完成。。。" + Thread.currentThread().getName());


    }

    public synchronized void method2() {
        System.out.println("加锁的方法2：" + Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("加锁方法完成。。。" + Thread.currentThread().getName());

    }

    public static void main(String[] args) {


        Thread thread = new Thread(instance);

        Thread thread2 = new Thread(instance);

        thread.start();
        thread2.start();

        while (thread.isAlive() || thread.isAlive()) {

        }

        System.out.println("Finished .....");

    }
}
